import {
  observable,
  action
} from 'mobx-miniprogram'
import { getUserInfo } from '../pages/api/index'

export const store = observable({
  userInfo: null,
  active: "index",
  activeTable: wx.getStorageSync('activeTable') || 0,
  selectAddrData: null,
  selectCoupon: null,
  orderData: null,
  updateActive: action(function (val) {
    this.active = val
  }),
  updateSelectAddrData: action(function (val) {
    this.selectAddrData = val;
  }),
  updateSelectCoupon: action(function (val) {
    this.selectCoupon = val;
  }),
  getUserInfo: action(function () {
    return new Promise((resolve, reject) => {
      getUserInfo().then(res => {
        this.updateUserInfo(res.result);
        resolve(res.result);
      });
    });
  }),
  updateOrderData: action(function (val) {
    this.orderData = val;
  }),
  updateActiveTable: action(function (val) {
    wx.setStorageSync('activeTable', val);
    this.activeTable = val;
  }),
  updateUserInfo: action(function (userInfo) {
    this.userInfo = userInfo;
  })
})